//
//  BaseTabBarController.m
//  BaseFramework01
//
//  Created by IMAC on 2017/6/1.
//  Copyright © 2017年 IMAC. All rights reserved.
//

#import "BaseTabBarController.h"
#import "RootVC.h"
#import "TVVC.h"
#import "LibraryVC.h"
#import "DiscoveryVC.h"
#import "UserVC.h"
#import "TabBarButton.h"

#import <BaiduMapAPI_Location/BMKLocationComponent.h>//引入定位功能所有的头文件

@interface BaseTabBarController ()

@end

#define DEFAULTSELECTINDEX 0

#define btn_tag 1000

@implementation BaseTabBarController


static BaseTabBarController *sharedBaseTabBar;
+ (BaseTabBarController *)sharedBaseTabBarViewController {
    
    //	static dispatch_once_t onceToken;
    //	dispatch_once(&onceToken, ^{
    if (!sharedBaseTabBar) {
        sharedBaseTabBar = [[BaseTabBarController alloc] init];
        NSLog(@"初始化");
    }
    //	});
    return sharedBaseTabBar;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
//    [self getLocation];
    [self creatMainView];
}
-(void)creatMainView{
    
    
    NSArray *titleArr =@[@"首页",@"电视",@"片库",@"发现",@"我的"];
    
    RootVC * vc1 = [[RootVC alloc] init];
    
    TVVC* vc2 = [[TVVC alloc]init];
    
    LibraryVC *vc3 = [[LibraryVC alloc]init];
    
    DiscoveryVC *vc4 = [[DiscoveryVC alloc]init];
    
    UserVC *vc5 = [[UserVC alloc]init];
    
    self.viewControllers = @[vc1,vc2,vc3,vc4,vc5];
    
    self.bgView = [[UIView alloc] initWithFrame:CGRectMake(0,SCREEN_HEIGHT-UI_TAB_BAR_HEIGHT, SCREEN_WIDTH, UI_TAB_BAR_HEIGHT)];
    self.tabBar.hidden = YES;
    _bgView.backgroundColor = RGB(240, 240, 240);
    _bgView.userInteractionEnabled = YES;
    [self.view addSubview:_bgView];
    _bgView.layer.shadowColor = [UIColor blackColor].CGColor;
    _bgView.layer.shadowOpacity = 0.6;
    _bgView.layer.shadowOffset = CGSizeMake(-2,2);
    _bgView.layer.shadowRadius = 2;
    
    
    for (int i = 0; i < titleArr.count; i ++)
    {
        TabBarButton *button = [TabBarButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(i*SCREEN_WIDTH/(titleArr.count),0,SCREEN_WIDTH/(titleArr.count),UI_TAB_BAR_HEIGHT);
        button.enabled = i==DEFAULTSELECTINDEX?NO:YES;
        [button setImage:[UIImage imageNamed:[NSString stringWithFormat:@"tabbar%d",i+1]] forState:UIControlStateNormal];
        [button setImage:[UIImage imageNamed:[NSString stringWithFormat:@"tabbar%d_selected",i+1]] forState:UIControlStateDisabled];
        [button setTitle:titleArr[i] forState:UIControlStateNormal];
        [button setTitleColor:RGB(114, 114, 114) forState:UIControlStateNormal];
        [button setTitleColor:RGB(249, 131, 35) forState:UIControlStateDisabled];
        button.titleLabel.font =[UIFont systemFontOfSize:10];
        button.titleLabel.textAlignment =NSTextAlignmentCenter;
        [button setImage:[UIImage imageNamed:[NSString stringWithFormat:@"tabbar%d_selected",i+1]] forState:UIControlStateDisabled];
        button.contentMode =UIViewContentModeCenter;
        button.clipsToBounds =YES;
        
        [button addTarget:self action:@selector(chooseTabBar:) forControlEvents:UIControlEventTouchUpInside];
        button.adjustsImageWhenHighlighted =NO;
        button.tag =btn_tag+i;
        if (!button.enabled){
            [self setSelectedIndex:i];
            _beforeIndex = i;
            _presentIndex = i;
        }
        [_bgView addSubview:button];
            
        
    }

    
}

-(void)chooseTabBar:(UIButton *)sender
{
    _presentIndex = sender.tag-btn_tag;
    [self confirmSelectTabBar:_presentIndex];
    [self setSelectedIndex:_presentIndex];
    _beforeIndex = _presentIndex;
}

-(void)confirmSelectTabBar:(NSInteger)selectIndex
{
    for (UIButton * button in [_bgView subviews]){
        if ([button isKindOfClass:[UIButton class]]){
            button.enabled= button.tag==selectIndex+btn_tag?NO:YES;
        }
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
